package com.blog.backend.service.impl;

import com.blog.backend.domain.BlogPerson;
import com.blog.backend.domain.BlogPersonWithBLOBs;
import com.blog.backend.mapper.BlogFileMapper;
import com.blog.backend.mapper.BlogPersonMapper;
import com.blog.backend.mapper.ImageMapper;
import com.blog.backend.model.BlogFile;
import com.blog.backend.req.PersonReq;
import com.blog.backend.resp.PersonResp;
import com.blog.backend.resp.PictureUrlResp;
import com.blog.backend.service.PersonInfoService;
import com.blog.backend.utils.UuidUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author fanggs
 * @Date 2021/7/16 0016 14:18
 */
@Service
@Slf4j
public class PersonInfoServiceImpl implements PersonInfoService {

    @Resource
    private BlogPersonMapper blogPersonMapper;

    @Resource
    private BlogFileMapper blogFileMapper;

    @Resource
    private ImageMapper imageMapper;



    @Override
    public void updateInfos(PersonReq req) {
        log.info("修改个人信息:{}",req);
        BlogPerson blogPerson=blogPersonMapper.selectByPrimaryKey();
        BlogPersonWithBLOBs info = new BlogPersonWithBLOBs();
        BeanUtils.copyProperties(req,info);
        if (ObjectUtils.isEmpty(blogPerson)){
            info.setPersonId(UuidUtils.getId());
            int i = blogPersonMapper.insertSelective(info);
        }else {

            blogPersonMapper.updateByPrimaryKeySelective(info);
        }
    }

    @Override
    public PersonResp queryDetails() {
        log.info("查看个人信息详情");
        PersonResp resp = new PersonResp();
        BlogPersonWithBLOBs blogPerson = blogPersonMapper.selectByPrimaryKey();
        if (ObjectUtils.isEmpty(blogPerson)){
            return resp;
        }else {
            //查看头像地址
            BeanUtils.copyProperties(blogPerson,resp);
            if (StringUtils.isNotBlank(resp.getProfile())){
                List<PictureUrlResp> strings = imageMapper.selectImageList(resp.getProfile());
                resp.setImageList(strings);
            }
        }
        return resp;
    }

}
